/**
 * Created by 鹏 on 14-3-11.
 */
var userService = require('../Service/userService.js');
var enums = require('../common/enum');

exports.verify = function (req, res) {
    var email = req.param('Email');
    var password = req.param('Password');
    userService.verify(email, password, function (exist) {
        if (exist) {
            userService.getUser(email, function (user) {
                req.session.User = user;
                res.json({isMatch: exist});
            });
        }
        else {
            res.json({isMatch: exist});
        }
    });
};

exports.signUp = function (req, res) {
    res.render("signUp");
};

exports.create = function (req, res) {
    var newUser = {
        Email: req.param("Email"),
        Nick: req.param('Nick'),
        Password: req.param("Password"),
        Status: enums.UserStatus.Enabled,
        IsAdmin: enums.IsAdmin.No
    };
    userService.getUser(newUser.Email, function (user) {
        if (user == null) {
            userService.signUp(newUser, function () {
                userService.getUser(newUser.Email, function (createdUser) {
                    req.session.User = createdUser;
                    res.json({created: true});
                });
            });
        }
        else {
            res.json({created: false});
        }
    });
};